﻿using Boomerang.Common.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Boomerang.Common.Enumerations;
namespace BoomerangTests
{
    
    
    /// <summary>
    ///This is a test class for ConfigurationManagerTest and is intended
    ///to contain all ConfigurationManagerTest Unit Tests
    ///</summary>
    [TestClass()]
    public class ConfigurationManagerTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        [TestInitialize()]
        public void MyTestInitialize()
        {
            Organization test = new Organization();
            
            test.CrmConnectionOptions.AuthMode = CrmConnectionType.IFD;
            test.CrmConnectionOptions.DiscoveryServerUrl = "http://test/";
            test.CrmConnectionOptions.Domain = "test";
            test.CrmConnectionOptions.Password = "test";
            test.CrmConnectionOptions.UserName = "test";
            testContextInstance.Properties["testOrg"] = test;
        }
        //
        //Use TestCleanup to run code after each test has run
        [TestCleanup()]
        public void MyTestCleanup()
        {
        }
        //
        #endregion


        /// <summary>
        ///A test for SetOrganization
        ///</summary>
        [TestMethod()]
        public void SetOrganizationTest()
        {
            ConfigurationManager_Accessor.organizations = new System.Collections.Generic.Dictionary<string, Organization>();
            Organization org = (Organization)testContextInstance.Properties["testOrg"];
            ConfigurationManager.SetOrganization(org);
            Assert.AreEqual<int>(ConfigurationManager_Accessor.organizations.Count, 1);
        }

        /// <summary>
        ///A test for GetOrganization
        ///</summary>
        [TestMethod()]
        public void GetOrganizationTest()
        {
            Organization expected = (Organization)testContextInstance.Properties["testOrg"];
            Organization actual;
            ConfigurationManager.SetOrganization(expected);
            actual = ConfigurationManager.GetOrganization(expected.Key);
            Assert.AreEqual(expected, actual);            
        }

        /// <summary>
        ///A test for Load
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Boomerang.Common.dll")]
        public void LoadTest()
        {
            Organization test = (Organization)testContextInstance.Properties["testOrg"];

            ConfigurationManager.SetOrganization(test);           
            ConfigurationManager.Save();
            ConfigurationManager_Accessor.Load();

            Assert.AreEqual<Organization>(test, ConfigurationManager.GetOrganization(test.Key), "Failed to Load and organization");
            
        }

        /// <summary>
        ///A test for Save
        ///</summary>
        [TestMethod()]
        public void SaveTest()
        {
            try
            {
                ConfigurationManager.SetOrganization((Organization)testContextInstance.Properties["testOrg"]);
                ConfigurationManager.Save();
            }
            catch
            {
                Assert.Fail();
            }
        }
    }
}
